package com.lanju.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lanju.common.SpringUtils;
import com.lanju.dao.AgreementDao;
import com.lanju.entity.Agreement;
import com.lanju.entity.User;
import com.lanju.model.ResultModel;
import com.lanju.service.AgreementService;
import com.lanju.service.JedisService;

@Controller
@RequestMapping("/agreement")
public class AgreementController {
	private static final Logger LOGGER = LoggerFactory.getLogger(AgreementController.class);
    
    @Autowired
    AgreementService agreementService;
    
    @Autowired
    AgreementDao agreementDao;
    
    @Resource
   	JedisService jedisService;
    
    
    @RequestMapping("/edit")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel edit(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Agreement agreement = json.toJavaObject(Agreement.class);
		User user =(User) session.getAttribute("userModel");
		agreement.setUpdator(user.getUserId());
		agreement.setUpdateTime(new Date());
        return  agreementService.edit(agreement);
    }
    
    @RequestMapping("/editAgreementContent")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel editAgreementContent(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Agreement agreement = json.toJavaObject(Agreement.class);
		 User user =(User) session.getAttribute("userModel");
		agreement.setUpdator(user.getUserId());
		agreement.setUpdateTime(new Date());
        return  agreementService.editAgreementContent(agreement);
    }
    
    @RequestMapping("/getById")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Agreement getById(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Agreement agreement = json.toJavaObject(Agreement.class);
        return  agreementService.getById(agreement.getAgreementId());
    }
    
    
    /** 根据参数获取 */
    @RequestMapping("/getByParam")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Agreement getByParam(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(param));
		Agreement agreement = json.toJavaObject(Agreement.class);
        return  agreementService.getByParam(agreement);
    }
    
    
    /** 获取列表 */
    @RequestMapping("/getList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map<String, Object> getList(HttpServletRequest request, HttpServletResponse response,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		if( "1".equals(param.get("userIdFlag")) ){
			 User user =(User) session.getAttribute("userModel");
			param.put("userId", user.getUserId());
		}
		Integer pageSize = Integer.parseInt( (String)param.get("pageSize") );
		Integer pageNumber = Integer.parseInt( (String)param.get("pageNumber") );
		Integer offset = (pageNumber-1)*pageSize;
        return  agreementService.getList(param, pageSize, offset);
    }
    
    @RequestMapping("/signName")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public ResultModel signName(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
        return  agreementService.signName(param);
    }
    /**
     * 获取所有合同的
     * @return
     */
    @RequestMapping("/getAllAgreementList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Map<String,Object> getAllAgreementList(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
		return null;
    }

    @RequestMapping("getAgreementById")  
    @ResponseBody
    public Map<String,Object> getAgreementById(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
		return agreementService.getAgreementById(param);	
    }
}
